﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Configuration;
using System.Drawing;

namespace GenerateSmallPhotos
{
    class Program
    {
        static CalligraphyEntities _context = new CalligraphyEntities();
        
        static void Main(string[] args)
        {
            
                GetRawPhotos(250);
                GetRawPhotos(400);
                GetRawPhotos(110);
        }

        static void GetRawPhotos(int destLenght)
        {
            string strDestLenght = destLenght.ToString();
            var q = from p in _context.T_Photo where (p.IsDeleted == null || p.IsDeleted == false) && p.Size.Contains("raw") && (from sp in p.T_Photo1 where sp.Size.Contains(strDestLenght) select sp).Count() == 0 select p;
            var rawPhotoes = q.ToArray<T_Photo>();
            foreach (var rawP in rawPhotoes)
            {
                CreateSmallPhoto(rawP, destLenght);
            }
        }

       

        static string GetFileNameExt(T_Photo photo)
        {
            var strs = photo.ContentType.Split('/');
            return strs[1];
        }

        static void CreateSmallPhoto(T_Photo rawPhoto, int destLenght)
        {
            string sourcePath = GetFilePath(rawPhoto);
            Console.WriteLine("源图片路径是" + sourcePath);
            try
            {
                Image sourceImage = Image.FromFile(sourcePath);

                T_Photo smallPhoto = new T_Photo();
                smallPhoto.RawPhotoID = rawPhoto.ID;
                smallPhoto.T_Photo2 = rawPhoto;
                smallPhoto.ContentType = "imgae/jpg";
                smallPhoto.Width = destLenght;
                smallPhoto.Height = destLenght;
                smallPhoto.Size = destLenght.ToString();
                _context.T_Photo.Add(smallPhoto);
                _context.SaveChanges();

                if (sourceImage.Width < destLenght || sourceImage.Height < destLenght)
                {

                    System.IO.File.Copy(GetFilePath(rawPhoto), GetFilePath(smallPhoto));
                }
                else
                {
                    float wideRatio = (float)destLenght / (float)sourceImage.Width;
                    float heightRatio = (float)destLenght / (float)sourceImage.Height;

                    int resizeWide, resizeHeight;

                    if (wideRatio > heightRatio)
                    {
                        resizeWide = destLenght;
                        resizeHeight = (int)((float)sourceImage.Height * wideRatio);
                    }
                    else if (wideRatio == heightRatio)
                    {
                        resizeWide = destLenght;
                        resizeHeight = destLenght;
                    }
                    else
                    {
                        resizeWide = (int)((float)sourceImage.Width * heightRatio);
                        resizeHeight = destLenght;
                    }

                    Image resizedImage = DrawImage(sourceImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), new Rectangle(0, 0, resizeWide, resizeHeight));

                    Image cutedImage;
                    if (wideRatio == heightRatio)
                    {
                        cutedImage = resizedImage;
                    }
                    else
                    {
                        cutedImage = DrawImage(resizedImage, new Rectangle(0, 0, destLenght, destLenght), new Rectangle(0, 0, destLenght, destLenght));
                    }


                    string destFilePath = GetFilePath(smallPhoto);
                    cutedImage.Save(destFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
            
        }

        static Image DrawImage(Image sourImage, Rectangle sourRect, Rectangle destRect)
        {
            Image destImage = new Bitmap(destRect.Width, destRect.Height);
            using (Graphics g = Graphics.FromImage(destImage))
            {
                g.DrawImage(sourImage, destRect, sourRect,  GraphicsUnit.Pixel);
            }
            return destImage;
        }

        static string GetFilePath(T_Photo p)
        {
            string photoBasePath = System.Configuration.ConfigurationSettings.AppSettings["PhotoBasePath"];
            return photoBasePath + p.ID.ToString() + "." + GetFileNameExt(p);
        }
    }
}
